﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CiStatus.CiStatusAddIn.Logic;

namespace CiStatus.CiStatusAddIn.Views.ToolsOptionsViews
{
    public partial class OptionsCompositeControl : UserControl
    {
        private readonly ISettingsFormatter _formatter;
        private IDictionary<string, string> _settings = new Dictionary<string, string>();
        private IDictionary<KeyValuePair<string, string>, IDictionary<string, string>> _allSettings;
        private OptionsPage _optionsPage;

        public OptionsCompositeControl(ISettingsFormatter formatter)
        {
            _formatter = formatter;
            InitializeComponent();
        }

        public OptionsPage OptionsPage
        {
            get { return _optionsPage; }
            set 
            { 
                _optionsPage = value;
                PluginPath = value.PluginPath;
                PollingFrequence = value.PollingFrequence;
                _allSettings = _formatter.Deserialize(value.Settings);
            }
        }


        private void OptionsCompositeControlLoad(object sender, EventArgs e)
        {
            UpdateProjectListBox();
            if (lbProjects.Items.Count > 0)
                lbProjects.SelectedIndex = 0;
            lbKeyValue.Sorted = true;
            txtPluginPath.Text = PluginPath;

            //Argh need to put this logic in its own object somewhere
            if (PollingFrequence > 0)
            {
                cmbPollingFrequence.SelectedItem = PollingFrequence.ToString();
            }
            else
            {
                cmbPollingFrequence.SelectedItem = "60";
            }
        }

        private void UpdateProjectListBox()
        {
            lbProjects.Items.Clear();
            foreach (var setting in _allSettings)
            {
                lbProjects.Items.Add(setting.Key);
            }
        }

        private void LbKeyValueMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (lbKeyValue.SelectedItem == null) return;

            var key = (KeyValuePair<string, string>) lbKeyValue.SelectedItem;
            using (var addKeyValue = new AddSettingForm(key))
            {
                if (addKeyValue.ShowDialog() != DialogResult.OK) return;
                _settings.Remove(key);
                _settings.Add(addKeyValue.Pair); 
            }
            UpdateSettingsListBox();
        }

        private void BtnAddSettingClick(object sender, EventArgs e)
        {
            if (lbProjects.SelectedItems.Count > 0)
            {
                using (var addKeyValue = new AddSettingForm())
                {
                    if (addKeyValue.ShowDialog() != DialogResult.OK) return;

                    _settings.Add(addKeyValue.Pair);
                }
                UpdateSettingsListBox();
            }
        }

        private void BtnRemoveSettingClick(object sender, EventArgs e)
        {
            if (lbKeyValue.SelectedItem != null)
            {
                _settings.Remove((KeyValuePair<string, string>) lbKeyValue.SelectedItem);
            }
            UpdateSettingsListBox();
        }
        
        public string Settings
        {
            get { return _formatter.Serialize(_allSettings); }
        }

        private void UpdateSettingsListBox()
        {
            lbKeyValue.Items.Clear();
            foreach (var setting in _settings)
            {
                lbKeyValue.Items.Add(setting);
            }
        }

        private void btnAddNewProject_Click(object sender, EventArgs e)
        {
            using (var newProject = new NewProjectForm(PluginPath))
            {
                if (newProject.ShowDialog(this) == DialogResult.OK)
                {
                    if (!_allSettings.ContainsKey(newProject.Project))
                    {
                        lbProjects.Items.Add(newProject.Project);
                        _allSettings.Add(newProject.Project, newProject.Settings);
                        lbProjects.SelectedItem = newProject.Project;
                    }
                }
            }
        }

        public string PluginPath { get; set; }

        public int PollingFrequence { get; set; }

        private void lbProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            _settings = _allSettings[(KeyValuePair<string,string>)lbProjects.SelectedItem];
            UpdateSettingsListBox();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (var browserDialog = new FolderBrowserDialog())
            {
                if (browserDialog.ShowDialog(this) == DialogResult.OK)
                {
                    txtPluginPath.Text = browserDialog.SelectedPath;
                    PluginPath = browserDialog.SelectedPath + "\\";
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lbProjects.SelectedItems.Count > 0)
            {
                _allSettings.Remove((KeyValuePair<string, string>) lbProjects.SelectedItem);
                UpdateProjectListBox();
                lbKeyValue.Items.Clear();
            }
        }

        private void cmbPollingFrequence_SelectedIndexChanged(object sender, EventArgs e)
        {
            PollingFrequence = Convert.ToInt32(cmbPollingFrequence.Text);
        }
    }
}
